package com.logic.modular.game.battle.process.skillEffectHandle.impl;

import com.logic.modular.game.battle.process.skillEffectHandle.base.BaseSkillEffectStrategy;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import org.springframework.stereotype.Component;

import java.util.List;


public class DotStrategy {
    @Component
    public static class BaseDotStrategy extends BaseSkillEffectStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);
            apply(pvpSkillEffect);
            return pvpSkillEffect;
        }

        @Override
        public PvpSkillEffect apply(PvpSkillEffect pvpSkillEffect) {
            return null;
        }

        public PvpSkillEffect physicsApply(
                PvpSkillEffect pvpSkillEffect

        ) {
            if (pvpSkillEffect.status.getIsTrigger() != 1) {
                attributeService.refreshPanel(pvpSkillEffect);
                return pvpSkillEffect;
            }
            String battleUUID = pvpSkillEffect.getBattleUUID();
            Long battleId = pvpSkillEffect.getBattleId();
            Long sourcePlayerHeroId = pvpSkillEffect.getSourcePlayerHeroId();
            Long targetPlayerHeroId = pvpSkillEffect.getTargetPlayerHeroId();
            PlayerHeroAttribute sourceAttribute = attributeService.get(battleUUID, sourcePlayerHeroId);
            PlayerHeroAttribute targetAttribute = attributeService.get(battleUUID, targetPlayerHeroId);
            Long coreSkillEffectTypeId = pvpSkillEffect.getCoreSkillEffectTypeId();
            String fieldName = pvpSkillEffect.getSkillEffectLayer().getName();
            Double value = pvpSkillEffect.getBaseEffectValueInfo().getBaseValue();

            double v = coreCombatFormulaService.physicsDotHurt(
                    //            long 进攻方兵力,
                    sourceAttribute.getCurrentForce(),
                    //             double 攻击方攻击,
                    sourceAttribute.getAtt(),
                    //             double 攻击方物理穿透,
                    sourceAttribute.getIgPhysicalDefense(),
                    //             double 攻击方战法加成,
                    value,
                    //             double 攻击方增减伤,
                    sourceAttribute.obtainStrategyAttackIncrease() - targetAttribute.obtainStrategyAttackDecrease(),
                    //             double 防守方防御
                    targetAttribute.getDef()
            );
            Integer triggerNum = pvpSkillEffect.getBaseTrigger().getTriggerNum();
            targetAttribute.attributeUpdate(
                    fieldName,
                    v,
                    0,
                    1,
                    coreSkillEffectTypeId);
            sourceAttribute.attributeUpdate(
                    fieldName,
                    v,
                    1,
                    1,
                    coreSkillEffectTypeId);

            attributeService.saveCache(sourceAttribute);
            attributeService.saveCache(targetAttribute);
            return pvpSkillEffect;
        }

        public PvpSkillEffect strategyApply(
                PvpSkillEffect pvpSkillEffect
        ) {
            if (pvpSkillEffect.status.getIsTrigger() != 1) {
                attributeService.refreshPanel(pvpSkillEffect);
                return pvpSkillEffect;
            }
            String battleUUID = pvpSkillEffect.getBattleUUID();
            Long battleId = pvpSkillEffect.getBattleId();
            Long sourcePlayerHeroId = pvpSkillEffect.getSourcePlayerHeroId();
            Long targetPlayerHeroId = pvpSkillEffect.getTargetPlayerHeroId();
            PlayerHeroAttribute sourceAttribute = this.attributeService.get(battleUUID, sourcePlayerHeroId);
            Long coreSkillEffectTypeId = pvpSkillEffect.getCoreSkillEffectTypeId();
            String fieldName = pvpSkillEffect.getSkillEffectLayer().getName();
            Integer isMagic = pvpSkillEffect.getIsMagic();
            Integer trigger6 = pvpSkillEffect.getBaseTrigger().getTrigger6();
            List<PlayerHeroAttribute> targetHeroes =
                    releaseSkillService2.getTargetHeroes(battleUUID, targetPlayerHeroId, trigger6);

            for (PlayerHeroAttribute targetAttribute : targetHeroes) {
                double value = pvpSkillEffect.getMagicInfluence() / 100.0;
                double v = coreCombatFormulaService.strategyDotHurt(
                        //            long 进攻方兵力,
                        sourceAttribute.getCurrentForce(),
                        //             double 攻击方策略,
                        sourceAttribute.getMagic(),
                        //             double 攻击方策略穿透,
                        sourceAttribute.getIgStrategicDefense(),
                        //             double 攻击方战法基础伤害率,
                        1,
                        //             double 攻击方战法伤害率成长,
                        value,
                        //             int 是否受策略影响,
                        isMagic,
                        //             double 攻击方增减伤,
                        sourceAttribute.obtainStrategyAttackIncrease() - targetAttribute.obtainStrategyAttackDecrease(),
                        //             double 防守方策略
                        targetAttribute.getMagic()
                );
                Integer triggerNum = pvpSkillEffect.getBaseTrigger().getTriggerNum();
                targetAttribute.attributeUpdate(
                        fieldName,
                        v,
                        0,
                        1,
                        coreSkillEffectTypeId);
                sourceAttribute.attributeUpdate(
                        fieldName,
                        v,
                        1,
                        1,
                        coreSkillEffectTypeId);
                attributeService.saveCache(sourceAttribute);
                attributeService.saveCache(targetAttribute);
            }
            pvpSkillEffect.status.addTriggers(1);
            attributeService.refreshPanel(pvpSkillEffect);
            return pvpSkillEffect;
        }
    }

    // 28	溃逃 dot伤害，每回合行动开始受到一次物理伤害
    @Component
    public static class Dot28 extends BaseDotStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            apply(pvpSkillEffect);
            return pvpSkillEffect;
        }

        public PvpSkillEffect apply(
                PvpSkillEffect pvpSkillEffect
        ) {
            return physicsApply(pvpSkillEffect);
        }
    }

    // 29	燃烧 dot伤害，每回合行动开始受到一次策略伤害
    @Component
    public static class Dot29 extends BaseDotStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            return super.applyEffect(pvpSkillEffect);
        }

        public PvpSkillEffect apply(
                PvpSkillEffect pvpSkillEffect
        ) {
            return strategyApply(pvpSkillEffect);
        }
    }

    // 30	妖术 dot伤害，每回合行动开始受到一次策略伤害
    @Component
    public static class Dot30 extends BaseDotStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            return super.applyEffect(pvpSkillEffect);
        }

        public PvpSkillEffect apply(
                PvpSkillEffect pvpSkillEffect
        ) {
            return strategyApply(pvpSkillEffect);
        }
    }

    // 31	恐慌 dot伤害，每回合行动开始受到一次策略伤害
    @Component
    public static class Dot31 extends BaseDotStrategy {
        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            return super.applyEffect(pvpSkillEffect);
        }

        public PvpSkillEffect apply(
                PvpSkillEffect pvpSkillEffect
        ) {
            return strategyApply(pvpSkillEffect);
        }
    }
}